3 use MediaWiki\Block\Restriction\PageRestriction
;
4 use MediaWiki\MediaWikiServices
;
5 use Wikimedia\TestingAccessWrapper
;
9 * @coversDefaultClass BlockListPager
11 class BlockListPagerTest
extends MediaWikiTestCase
{
14 * @covers ::formatValue
15 * @dataProvider formatValueEmptyProvider
16 * @dataProvider formatValueDefaultProvider
18 * @param string $value
19 * @param string $expected
21 public function testFormatValue( $name, $value, $expected, $row = null ) {
22 $this->setMwGlobals( [
23 'wgEnablePartialBlocks' => false,
25 $row = $row ?
: new stdClass
;
26 $pager = new BlockListPager( new SpecialPage(), [] );
27 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
28 $wrappedPager->mCurrentRow
= $row;
30 $formatted = $pager->formatValue( $name, $value );
31 $this->assertEquals( $expected, $formatted );
37 public function formatValueEmptyProvider() {
42 'Unable to format test',
46 wfTimestamp( TS_UNIX
),
52 'infinite<br />0 minutes left',
58 * Test the default row values.
60 public function formatValueDefaultProvider() {
63 'ipb_address' => '127.0.0.1',
64 'ipb_by_text' => 'Admin',
65 'ipb_create_account' => 1,
68 'ipb_create_account' => 1,
69 'ipb_enable_autoblock' => 1,
71 'ipb_block_email' => 0,
72 'ipb_allow_usertalk' => 0,
80 'Unable to format test',
85 wfTimestamp( TS_UNIX
),
92 'infinite<br />0 minutes left',
104 '<ul><li>account creation disabled</li><li>cannot edit own talk page</li></ul>',
111 * @covers ::formatValue
113 public function testFormatValueRestrictions() {
114 $this->setMwGlobals( 'wgArticlePath', '/wiki/$1' );
116 $pager = new BlockListPager( new SpecialPage(), [] );
121 'ipb_anon_only' => 0,
122 'ipb_enable_autoblock' => 0,
123 'ipb_create_account' => 0,
124 'ipb_block_email' => 0,
125 'ipb_allow_usertalk' => 1,
128 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
129 $wrappedPager->mCurrentRow
= $row;
131 $pageName = 'Victor Frankenstein';
132 $page = $this->insertPage( $pageName );
133 $title = $page['title'];
134 $pageId = $page['id'];
137 ( new PageRestriction( 0, $pageId ) )->setTitle( $title )
140 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
141 $wrappedPager->restrictions
= $restrictions;
143 $formatted = $pager->formatValue( 'ipb_params', '' );
144 $this->assertEquals( '<ul><li>'
145 // FIXME: Expectation value should not be dynamic
146 // and must not depend on a localisation message.
147 // TODO: Mock the message or consider using qqx.
148 . wfMessage( 'blocklist-editing' )->text()
149 . '<ul><li><a href="/wiki/Victor_Frankenstein" title="'
153 . '</a></li></ul></li></ul>',
159 * @covers ::preprocessResults
161 public function testPreprocessResults() {
162 // Test the Link Cache.
163 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
164 $wrappedlinkCache = TestingAccessWrapper
::newFromObject( $linkCache );
168 'User_talk:127.0.0.1',
173 foreach ( $links as $link ) {
174 $this->assertNull( $wrappedlinkCache->badLinks
->get( $link ) );
178 'ipb_address' => '127.0.0.1',
179 'by_user_name' => 'Admin',
181 'ipb_timestamp' => $this->db
->timestamp( wfTimestamp( TS_MW
) ),
183 $pager = new BlockListPager( new SpecialPage(), [] );
184 $pager->preprocessResults( [ $row ] );
186 foreach ( $links as $link ) {
187 $this->assertSame( 1, $wrappedlinkCache->badLinks
->get( $link ) );
190 // Test Sitewide Blocks.
192 'ipb_address' => '127.0.0.1',
193 'by_user_name' => 'Admin',
196 $pager = new BlockListPager( new SpecialPage(), [] );
197 $pager->preprocessResults( [ $row ] );
199 $this->assertObjectNotHasAttribute( 'ipb_restrictions', $row );
201 $pageName = 'Victor Frankenstein';
202 $page = $this->getExistingTestPage( 'Victor Frankenstein' );
203 $title = $page->getTitle();
205 $target = '127.0.0.1';
207 // Test Partial Blocks Blocks.
208 $block = new Block( [
209 'address' => $target,
210 'by' => $this->getTestSysop()->getUser()->getId(),
211 'reason' => 'Parce que',
212 'expiry' => $this->db
->getInfinity(),
215 $block->setRestrictions( [
216 new PageRestriction( 0, $page->getId() ),
220 $result = $this->db
->select( 'ipblocks', [ '*' ], [ 'ipb_id' => $block->getId() ] );
222 $pager = new BlockListPager( new SpecialPage(), [] );
223 $pager->preprocessResults( $result );
225 $wrappedPager = TestingAccessWrapper
::newFromObject( $pager );
227 $restrictions = $wrappedPager->restrictions
;
228 $this->assertInternalType( 'array', $restrictions );
230 $restriction = $restrictions[0];
231 $this->assertEquals( $page->getId(), $restriction->getValue() );
232 $this->assertEquals( $page->getId(), $restriction->getTitle()->getArticleId() );
233 $this->assertEquals( $title->getDBKey(), $restriction->getTitle()->getDBKey() );
234 $this->assertEquals( $title->getNamespace(), $restriction->getTitle()->getNamespace() );
236 // Delete the block and the restrictions.